<?php
$page_name=basename($_SERVER['SCRIPT_FILENAME']);
$IsPreserved	= 'Y';
$IsProcess		= $_REQUEST['IsProcess'];
include("general_include.php");
include "checklogin.php";
//if($_SESSION['user_type']=='2')
 // header("location:student_game.php");
if($IsProcess <> 'Y')
{
	include "top.php";
	//include "left.php";
}	

$action	= $_REQUEST['action'];

if($action=="del")
{	
	if(!empty($delete_id))
	{
	
	  $DelSql = "DELETE FROM ".TABLEPREFIX."_user_game where user_game_id='$delete_id'";
	  $UserManagerObjAjax->Execute($DelSql);
	  
    }	  	 
}  

function findvalue($table,$fld_name,$fld_value,$find_value)
{
     if($fld_value=='')
			return false;
			
		$SelectTypeSql = "SELECT ".$find_value." FROM ".$table." WHERE $fld_name=".$fld_value;
		$SelectType = mysql_fetch_array(mysql_query($SelectTypeSql));
		return $SelectType[0];
}	


$TpgameSql = "SELECT * FROM ".TABLEPREFIX."_user_game where user_id='".$_SESSION['user_id']."' and user_type_id='3' ORDER BY date_added DESC";
$TpgameArr = $UserManagerObjAjax->GetRecords("All",$TpgameSql);
$Numcoachgame=count($TpgameArr);

for($g=0;$g<$Numcoachgame;$g++)
{
    $gameSql = "SELECT * FROM ".TABLEPREFIX."_game WHERE game_id='".$TpgameArr[$g]['game_id']."'";
	$GameArr = $UserManagerObjAjax->GetRecords("Row",$gameSql);
	$TpgameArr[$g]['is_ladder'] = $GameArr['is_ladder'];
	$TpgameArr[$g]['is_race'] = $GameArr['is_race'];
	$TpgameArr[$g]['is_server'] = $GameArr['is_server'];
	$TpgameArr[$g]['is_region'] = $GameArr['is_region'];
	$TpgameArr[$g]['is_rating'] = $GameArr['is_rating'];
    
    $TpgameArr[$g]['game']= findvalue(TABLEPREFIX."_game","game_id",$TpgameArr[$g]['game_id'],"game_name");
	$TpgameArr[$g]['ladder']= findvalue(TABLEPREFIX."_game_ladder","ladder_id",$TpgameArr[$g]['ladder_id'],"ladder_name");
	
	$Tprace = explode(",",$TpgameArr[$g]['race_id']);
	$race ='';
	for($a=0;$a<count($Tprace);$a++)
	{
	  if($Tprace[$a]<>'')
	   $race .= findvalue(TABLEPREFIX."_game_race","race_id",$Tprace[$a],"race_title").',';
	}
	$TpgameArr[$g]['race'] = substr($race,0,-1);  
	
	$Tpserver = explode(",",$TpgameArr[$g]['server_id']);
	$server ='';
	for($a=0;$a<count($Tpserver);$a++)
	{
	  if($Tpserver[$a]<>'')
	   $server .= findvalue(TABLEPREFIX."_game_server","server_id",$Tpserver[$a],"server_name").',';
	}
	$TpgameArr[$g]['server'] = substr($server,0,-1);  
	
	$Tpregion = explode(",",$TpgameArr[$g]['region_id']);
	$region ='';
	for($a=0;$a<count($Tpregion);$a++)
	{
	  if($Tpregion[$a]<>'')
	   $region .= findvalue(TABLEPREFIX."_game_region","region_id",$Tpregion[$a],"region_title").',';
	}
	$TpgameArr[$g]['region'] = substr($region,0,-1);
	
	$Tprating = explode(",",$TpgameArr[$g]['rating_id']);
	$rating ='';
	for($a=0;$a<count($Tprating);$a++)
	{
	  if($Tprating[$a]<>'')
	   $rating .= findvalue(TABLEPREFIX."_game_rating","rating_id",$Tprating[$a],"rating_title").',';
	}
	$TpgameArr[$g]['rating'] = substr($rating,0,-1);
	
	$Tprace[$g][$a] = substr($race,0,-1);  
	$TpgameArr[$g]['experience']= stripslashes($TpgameArr[$g]['experience']);

}

if($IsProcess == 'Y')
{
   $Sqltype = "SELECT * FROM ".TABLEPREFIX."_user_type_user_relation WHERE user_id='".$_SESSION['user_id']."'";
	$typeArr = $UserManagerObjAjax->GetRecords("All",$Sqltype);
	$Numtype = count($typeArr);
	$is_coach = 0;
	$is_partner = 0;
	for($t=0;$t<$Numtype;$t++)
	{
	   if($typeArr[$t]['user_type_id']==1)
		  $is_coach = 1;
	   if($typeArr[$t]['user_type_id']==3)
		  $is_partner = 1;
	}
}
$smarty->assign('is_coach',$is_coach);
$smarty->assign('is_partner',$is_partner);

$smarty->assign('Numcoachgame',$Numcoachgame);
$smarty->assign('TpgameArr',$TpgameArr);
$smarty->display('training_partner_game.tpl');
if($IsProcess <> 'Y')
{
	include "footer.php";
}	
?>